package com.inspur.icity.bus.api.user.service;

import com.inspur.icity.bus.api.user.model.BusSite;
import com.inspur.icity.bus.api.user.repo.BusSiteDao;
import com.inspur.icity.bus.base.core.service.CrudService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 站点Service
 */
@Service
public class BusSiteService extends CrudService<BusSiteDao, BusSite, Integer> {

    @Autowired
    private BusSearchHistoryService busSearchHistoryService;

    /**
     * 获取站点详情
     * @param name
     * @param areacode
     * @return
     */
    public Map<String,Object> getSiteDetails(String name, String areacode, String flag, String mobile) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("stationName",name.replace("站",""));
        queryMap.put("areacode",areacode);
        List <BusSite> busSites = find(queryMap);
        if(null == busSites || busSites.size() <= 0){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("name",busSites.get(0).getStationName());
        Map<String,Object> map = null;
        List<Map<String,Object>> list = new ArrayList<>();
        for (BusSite busSite : busSites){
            map = new HashMap<>();
            map.put("routeName",busSite.getRouteName());
            map.put("direction",busSite.getDirection());
            map.put("areacode",busSite.getAreacode());
            map.put("updown",busSite.getUpdown());
            list.add(map);
        }
        resultMap.put("routeList",list);

        //保存当前用户搜索站点
        if(null != flag && "search".equals(flag)){
            busSearchHistoryService.saveSearchHistory(name,null,1,null,2,mobile,areacode);
        }
        return resultMap;
    }

    /**
     * 查询站点信息（搜索框调用）
     * @param queryMap
     * @param result
     */
    public void queryBusSite(Map<String, Object> queryMap, List<Map<String, Object>> result) {
        queryMap.put("group","GROUP BY stationName"); //站点去重
        List<BusSite> list = find(queryMap);
        Map<String,Object> map = null;
        for (BusSite busSite : list){
            map = new HashedMap();
            map.put("type",2);//表示站点
            map.put("name",busSite.getStationName());
            result.add(map);
        }
    }

    /**
     * 批量保存
     * @param list
     */
    public void batchSave(List list){
        curdRepo.batchSave(list);
    }

    /**
     * 根据参数获取站点个数
     * @param name
     * @param areacode
     * @return
     */
    public int getSiteCount(String name, String areacode) {
        return curdRepo.getSiteCount(name,areacode);
    }

    /**
     * 根据系统代码删除站点信息
     * @param codes
     */
    public void removeByAreacode(Set<String> codes) {
        curdRepo.removeByAreacode(codes);
    }

    /**
     * 根据系统代码删除站点信息
     * @param code
     */
    public void removeByAreacode(String code) {
        Set<String> codes = new HashSet<>();
        codes.add(code);
        curdRepo.removeByAreacode(codes);
    }

    /**
     * 删除站点
     * @param busStations
     */
    public void batchDelete(List<BusSite> busStations) {
        for (BusSite busSite : busStations){
            curdRepo.removeByBusSite(busSite.getStationIndex(),busSite.getRouteId(),busSite.getAreacode());
        }
    }
}